/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.datasource.DataSourceManager;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.type.DataSourceType;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CreateDataSource
/*     */   extends MailDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException, SoapFaultException
/*     */   {
/*  45 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  46 */     Provisioning prov = Provisioning.getInstance();
/*  47 */     Account account = getRequestedAccount(zsc);
/*     */     
/*  49 */     if (!canModifyOptions(zsc, account)) {
/*  50 */       throw ServiceException.PERM_DENIED("can not modify options");
/*     */     }
/*  52 */     Mailbox mbox = getRequestedMailbox(zsc);
/*     */     
/*     */ 
/*  55 */     Element eDataSource = getDataSourceElement(request);
/*  56 */     DataSourceType type = DataSourceType.fromString(eDataSource.getName());
/*  57 */     Map<String, Object> dsAttrs = new HashMap();
/*     */     
/*     */ 
/*  60 */     validateFolderId(account, mbox, eDataSource, type);
/*  61 */     String name = eDataSource.getAttribute("name");
/*  62 */     dsAttrs.put("zimbraDataSourceFolderId", eDataSource.getAttribute("l"));
/*  63 */     dsAttrs.put("zimbraDataSourceEnabled", LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool("isEnabled")));
/*     */     
/*  65 */     dsAttrs.put("zimbraDataSourceImportOnly", LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool("importOnly", false)));
/*     */     
/*  67 */     dsAttrs.put("zimbraDataSourceHost", eDataSource.getAttribute("host"));
/*  68 */     dsAttrs.put("zimbraDataSourcePort", eDataSource.getAttribute("port"));
/*  69 */     dsAttrs.put("zimbraDataSourceConnectionType", eDataSource.getAttribute("connectionType"));
/*  70 */     dsAttrs.put("zimbraDataSourceUsername", eDataSource.getAttribute("username"));
/*  71 */     dsAttrs.put("zimbraDataSourcePassword", eDataSource.getAttribute("password"));
/*     */     
/*  73 */     String defaultSignature = eDataSource.getAttribute("defaultSignature", null);
/*  74 */     if (defaultSignature != null) {
/*  75 */       dsAttrs.put("zimbraPrefDefaultSignatureId", defaultSignature);
/*     */     }
/*     */     
/*  78 */     String forwardReplySignature = eDataSource.getAttribute("forwardReplySignature", null);
/*  79 */     if (forwardReplySignature != null) {
/*  80 */       dsAttrs.put("zimbraPrefForwardReplySignatureId", forwardReplySignature);
/*     */     }
/*     */     
/*     */ 
/*  84 */     dsAttrs.put("zimbraDataSourceType", type.toString());
/*     */     
/*     */ 
/*  87 */     String importClass = eDataSource.getAttribute("importClass", DataSourceManager.getDefaultImportClass(type));
/*  88 */     if (importClass != null) {
/*  89 */       dsAttrs.put("zimbraDataSourceImportClassName", importClass);
/*     */     }
/*     */     
/*     */ 
/*  93 */     ModifyDataSource.processCommonOptionalAttrs(dsAttrs, eDataSource);
/*     */     
/*     */ 
/*  96 */     if (type == DataSourceType.pop3) {
/*  97 */       dsAttrs.put("zimbraDataSourceLeaveOnServer", LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool("leaveOnServer", true)));
/*     */     }
/*     */     
/*     */ 
/* 101 */     DataSource ds = prov.createDataSource(account, type, name, dsAttrs);
/* 102 */     ZimbraLog.addDataSourceNameToContext(ds.getName());
/*     */     
/*     */ 
/* 105 */     Element response = zsc.createElement(MailConstants.CREATE_DATA_SOURCE_RESPONSE);
/* 106 */     eDataSource = response.addElement(type.toString());
/* 107 */     eDataSource.addAttribute("id", ds.getId());
/*     */     
/* 109 */     return response;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   static Element getDataSourceElement(Element request)
/*     */     throws ServiceException
/*     */   {
/* 117 */     List<Element> subElements = request.listElements();
/* 118 */     if (subElements.size() != 1) {
/* 119 */       String msg = "Only 1 data source allowed per request.  Found " + subElements.size();
/* 120 */       throw ServiceException.INVALID_REQUEST(msg, null);
/*     */     }
/*     */     
/* 123 */     return (Element)subElements.get(0);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   static void validateFolderId(Account account, Mailbox mbox, Element eDataSource, DataSourceType dsType)
/*     */     throws ServiceException
/*     */   {
/* 132 */     int folderId = (int)eDataSource.getAttributeLong("l");
/* 133 */     String id = eDataSource.getAttribute("id", null);
/*     */     try
/*     */     {
/* 136 */       mbox.getFolderById(null, folderId);
/*     */     } catch (MailServiceException.NoSuchItemException e) {
/* 138 */       throw ServiceException.INVALID_REQUEST("Invalid folder id: " + folderId, null);
/*     */     }
/* 140 */     for (DataSource ds : account.getAllDataSources()) {
/* 141 */       if ((id == null) || (!ds.getId().equals(id))) {
/*     */         try
/*     */         {
/* 144 */           for (Folder fldr : mbox.getFolderById(null, ds.getFolderId()).getSubfolderHierarchy()) {
/* 145 */             if ((fldr.getId() == folderId) && (
/* 146 */               (!DataSourceType.pop3.equals(dsType)) || (!DataSourceType.pop3.equals(ds.getType()))))
/*     */             {
/*     */ 
/* 149 */               throw ServiceException.INVALID_REQUEST("Folder location conflict: " + fldr.getPath(), null);
/*     */             }
/*     */           }
/*     */         }
/*     */         catch (MailServiceException.NoSuchItemException e) {}
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/CreateDataSource.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */